;(function($){
	$.jgrid.extend({
		/**
		 * TODO:GridToForm是jqGrid原生的方法,这里重写此方法(grid.formedit.js)
		 */
		GridToForm : function( rowid, formid ) {
			return this.each(function(){
				var $t = this;
				if (!$t.grid) {return;}
				var rowdata = $($t).jqGrid("getRowDataById",rowid);
				if (rowdata) {
					for(var i in rowdata) {
						var $elm = $("[name="+$.jgrid.jqID(i)+"]",formid);
						
						if ( $elm.is("input:radio") || $elm.is("input:checkbox"))  {
							$elm.each( function() {
								if( $(this).val() === rowdata[i] || $(this).val() == rowdata[i].toString()) {
									$(this)[$t.p.useProp ? 'prop': 'attr']("checked",true);
								} else {
									$(this)[$t.p.useProp ? 'prop': 'attr']("checked", false);
								}
							});
						} else if($elm.is('select')){
							$elm.find("option[value='"+rowdata[i]+"']").attr('selected',true);
						} else if($elm.is('textarea')){
							$("[name="+$.jgrid.jqID(i)+"]",formid).text(rowdata[i]);
						} else {
						// this is very slow on big table and form.
							$("[name="+$.jgrid.jqID(i)+"]",formid).val(rowdata[i]);
						}
						//TODO:begin
						//checked 当复选框用作单选作用时,勾选复选框是value应为true/1/yes或其它,反选复选框时以为false/0/no.
						//<k:checkbox formatRules="{checkedValueMap:{checked:true,unChecked:false}}"/>,
						//<k:checkbox formatRules="{checkedValueMap:{checked:1,unChecked:0}}"/>,
						//<k:checkbox formatRules="{checkedValueMap:{checked:yes,unChecked:no}}"/>,
						//和jq-format-impl.js checkedValueMap 实现方法对应
						var formatRules = $.parseJSON($elm.attr("formatRules"));
						if(!$.isEmptyObject(formatRules) 
								&& $.isPlainObject(formatRules)
								&& !$.isEmptyObject(formatRules.checkedValueMap) 
								&& $.isPlainObject(formatRules.checkedValueMap)){
							
							for(var key in formatRules.checkedValueMap) {
								if(rowdata[i] == formatRules.checkedValueMap[key] || rowdata[i].toString() == formatRules.checkedValueMap[key].toString()){
									$elm.val(rowdata[i]);
									if(key == 'checked'){
										$elm[$t.p.useProp ? 'prop': 'attr']("checked", true);
									}else{
										$elm[$t.p.useProp ? 'prop': 'attr']("checked", false);
									}
									break;
								}
							}
						}
						//TODO:end
					}
				}
			});
		}
	});
})(jQuery);